package com.gxuwz.crawlers;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.gxuwz.dao.AcademyDao;
import com.gxuwz.dao.CatalogDao;
import com.gxuwz.model.Academy;
import com.gxuwz.model.Catalog;
import com.gxuwz.model.Const;

public class CatalogJsoupCrawler {

	private static Catalog catalog = null;
	private static List<Catalog> catalogs = new ArrayList<>();

	public static void save() {
		List<Academy> academies = AcademyDao.queryAll();
		for (int i = 0; i < academies.size(); i++) {
			Document doc = BasicJsoupCrawler.start(academies.get(i)
					.getAcademyURL());
			// 获取尾页的链接,以此判断是否有分页
			String lastPage = doc.select("[class=pagination]").select("a")
					.toString();

			if (StringUtils.isNotBlank(lastPage)) {
				hasPaging(doc, lastPage);
			} else {
				noPaging(doc);
			}
		}
		
		CatalogDao.save(catalogs); // 保存高校贴吧到数据库
	}

	public static String samePartUrl(String lastPage) {
		return lastPage.substring(0, lastPage.lastIndexOf("=") + 1);
	}

	public static int totalRecord(String lastPage) {
		return Integer
				.valueOf(lastPage.substring(lastPage.lastIndexOf("=") + 1));
	}

	public static void noPaging(Document doc) {
		Elements links = doc.select("[class=sub_dir_box]").select("td")
				.select("a");
		for (Element link : links) {
			if (StringUtils.isNotBlank(link.text())) {
				addRecordToCatalogs(doc,link);
			}
		}
	}

	public static void hasPaging(Document doc, String lastPage) {
		lastPage = doc.select("[class=pagination]").select("a").last()
				.attr("href");
		for (int j = 1; j <= totalRecord(lastPage); j++) {
			Document docPagination = BasicJsoupCrawler.start(Const.SITE
					+ samePartUrl(lastPage) + j);
			Elements links = docPagination.select("[class=sub_dir_box]")
					.select("td").select("a");
			for (Element link : links) {
				if (StringUtils.isNotBlank(link.text())) {
					addRecordToCatalogs(doc,link);
				}
			}
		}
	}
	
	public static void addRecordToCatalogs(Document doc,Element link){
		catalog = new Catalog();
		// 替换特殊字符，例如： king's←jojo
		catalog.setCatalogName(link.text().replace("'", "_"));
		catalog.setCatalogURL(link.attr("href"));
		catalog.setCatalogProvince(doc.select(
				"[id=cur_second_class]").text());
		catalogs.add(catalog);		
	}

}
